package com.leyou.item.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.bo.SpuBo;
import com.leyou.item.mapper.GoodsMapper;
import com.leyou.item.mapper.SkuMapper;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.mapper.StockMapper;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.pojo.Stock;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.beans.Transient;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;

    @Autowired
    private SpuDetailMapper spuDetailMapper;

    public PageResult<SpuBo> querySpuByPage(Integer page, Integer rows, String key, Boolean saleable) {
        //查询分页数据
        PageHelper.startPage(page, rows);

        //构造查询条件
        Example example = new Example(Spu.class);
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(key)) {
            criteria.andLike("title", "%" + key + "%");
        }
        if (saleable != null) {
            criteria.andEqualTo("saleable", saleable);
        }


        Page<Spu> spu = (Page<Spu>) this.goodsMapper.selectByExample(example);

        //查分类数据和品牌数据;;封装数据
        List<SpuBo> spuBos = spu.stream().map(spu1 -> {
            //封装数据
            SpuBo spuBo = new SpuBo();
            BeanUtils.copyProperties(spu1, spuBo);
            String bname = this.brandService.queryNameByBid(spu1.getBrandId());
            spuBo.setBname(bname);
            String cname = this.categoryService.queryNameByCids(Arrays.asList(spu1.getCid1(), spu1.getCid2(), spu1.getCid3()));
            spuBo.setCname(cname);
            return spuBo;

        }).collect(Collectors.toList());


        return new PageResult<>(spu.getTotal(), spuBos);


    }

    @Transient
    public void saveGoods(SpuBo spuBo) {

        //分别保存数据，到四个表中
        Spu spu = new Spu();
        spu.setCreateTime(new Date());
        spu.setSaleable(true);
        spu.setValid(true);
        spu.setLastUpdateTime(spu.getCreateTime());
        BeanUtils.copyProperties(spuBo, spu);
        this.goodsMapper.insertSelective(spu);

        SpuDetail spuDetail = new SpuDetail();
        BeanUtils.copyProperties(spuBo, spuDetail);
        spuDetail.setSpuId(spuBo.getId());
        this.spuDetailMapper.insertSelective(spuDetail);

        for (Sku sku : spuBo.getSkus()) {
            sku.setCreateTime(new Date());
            sku.setLastUpdateTime(sku.getCreateTime());
            sku.setEnable(true);
            this.skuMapper.insertSelective(sku);
            Stock stock = new Stock();
            stock.setSkuId(sku.getId());
            this.stockMapper.insertSelective(stock);


        }





    }
}
